﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ChooseAllOfFenZhiCommand : HYExternalCommand
	{
		public override Result HYExecute(ref string TranName)
		{
			TranName = "选择水管分支";
			SearchBranch searchBranch = new SearchBranch(YArchitech.Revit.ElementType.Pipe, YArchitech.Revit.ElementType.Sprinklers, base.UiDoc);
			searchBranch.PickFirstElement(new PipeFilter(), "请选择一根水管……");
			searchBranch.DoSearch();
			ElementNode listRoot = searchBranch.GetListRoot();
			List<PipeInfo> list = new List<PipeInfo>();
			int num = 0;
			this.AddElementNode(listRoot, ref list, ref num);
			if (num > 0)
			{
				List<Element> list2 = new List<Element>();
				foreach (PipeInfo pipeInfo in list)
				{
					list2.Add(pipeInfo.ENode.Element);
				}
				RevitVersionFuncs.SetSelection(base.Sel, list2);
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			return Autodesk.Revit.UI.Result.Failed;
		}

		public void AddElementNode(ElementNode eNode, ref List<PipeInfo> listInfo, ref int id)
		{
			if (eNode == null)
			{
				return;
			}
			PipeInfo pipeInfo = new PipeInfo();
			if (id == 0)
			{
				pipeInfo.ParentId = "";
			}
			else
			{
				pipeInfo.ParentId = eNode.Preview.Element.Id.ToString();
			}
			id++;
			pipeInfo.ElemId = eNode.Element.Id.ToString();
			pipeInfo.ENode = eNode;
			listInfo.Add(pipeInfo);
			this.AddElementNode(eNode.Next, ref listInfo, ref id);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.AddElementNode(eNode2, ref listInfo, ref id);
				}
			}
		}
	}
}
